load c:\manoj\projects\ace\OMNI_ELEC.mat
load c:\manoj\projects\ace\substorm;
% load c:\manoj\projects\substorm\selected_substorm selected_substorm;
% fday_substorm = selected_substorm;
%load c:\manoj\projects\ace\rand_substorm;
load  c:\manoj\projects\longp\final_coefficients a_n b_n ;
a = a_n;
b = b_n;
phase_delay = 17;%minutes
st = 14/24;
en = 20/24;
n_pred = 0;
i_counter=0;
radius_epoch = 1/24;

for i = 1:length(fday_substorm),
    
    
%     if fday_substorm(i)-floor(fday_substorm(i)) > st & ...
%             fday_substorm(i)-floor(fday_substorm(i)) < en,
%         

       L = ace_all(:,1)  > fday_substorm(i)-  ( radius_epoch + 2/24)  & ...
               ace_all(:,1) < fday_substorm(i) +  ( radius_epoch + 1/24);
                 
      ace_time = ace_all(L,1)+(phase_delay)/(60*24);%to delay ace data 
      ace_data = ace_all(L,2);
      
      if all(~isnan(ace_data)),
      
          n_pred = n_pred + 1;
      pred_julia =filter(b,a,ace_data);
      L = ace_time >= (fday_substorm(i) + 1e-5 ) - radius_epoch & ...
              ace_time <= (fday_substorm(i) + 1e-5 ) + radius_epoch;
          
      SEG_PRED_JULIA(n_pred,:) = pred_julia(L); 
    
      end;
         
    
       L = ace_all(:,1) > fday_substorm(i)-radius_epoch & ...
               ace_all(:,1) < fday_substorm(i)+radius_epoch;
                 
       if sum(L) > 20,
           DATA_STORM = ace_all(L,2);
           ace_storm_fday = floor(min(ace_all(L,1)));
           i_counter = i_counter+1;
           fprintf('FDAY = %d NUMBER OF DATA = %d\n',ace_storm_fday,sum(L));     
          
            if sum(L) > floor(radius_epoch*2*1440/5),
               kkk = find(L ==1);
               L(kkk(1)) = 0;
            end;
           
           
            
           SEG_ACE(i_counter,:) = ace_all(L,2);
           SEG_IMF(i_counter,:) = ace_gsm_bz(L);
           end;
           
           end;
         
plot((-radius_epoch*1440) + 2.5 : 5 :  (radius_epoch*1440) - 2.5 ,nanmean( SEG_ACE),'b');
hold on;
plot((-radius_epoch*1440) + 2.5 : 5 :  (radius_epoch*1440) - 2.5 ,nanmean( SEG_PRED_JULIA),'r');

